#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

//prototype function PrintHello
void *PrintHello(void *threadid);

//function PrintHello
void *PrintHello(void *threadid)
{
  int t = (int)threadid;

  //print message
  printf("Hello World! It's me, thread #%d!\n", t);

  //exit thread
  pthread_exit(NULL);
}

//function main
int main (int argc, char *argv[])
{
   //declare an array of threads
   pthread_t threads[NUM_THREADS];

   //for loop 
   for(int t = 0; t < NUM_THREADS; t++){
      //print message
      printf("In main(): creating thread %d\n", t);
      //create threads and each thread will call function PrintHello
      //an error is returned in variable rc if any occurs during thread creation 
      int rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      //add error handling: show an error if an error is returned when creating a thread 
      if (rc){                          
         printf("Error creating thread %d; Return code from pthread_create() is %d\n", t, rc);                            
      }     
   }

   /* Last thing that main() should do */
   pthread_exit(NULL);
}

